package com.zzx.threekingdom.judge;

import java.util.ArrayList;

import com.zzx.threekingdom.model.AbsPlayerModel;
import com.zzx.threekingdom.model.ActionInfo;

public abstract class AskAll
{
    public abstract void doAsk(AbsPlayerModel pm, Runnable doneR);
    
    public ActionInfo actionInfo = new ActionInfo();
    private ArrayList<AbsPlayerModel> askPms = null;
    private int askIndex = 0;
    private Runnable doneRunnable = null;
    private Runnable askNextRunnable = new Runnable() { public void run() { goOnAsk(); } };

    public void setDone(Runnable done) { doneRunnable = done; }
    
    public void beginAsk(ArrayList<AbsPlayerModel> ask, Runnable doneR)
    {
        askIndex = 0;
        doneRunnable = doneR;
        askPms = ask;
        askCurrent();
    }
    
    public void askCurrent()
    {
        AbsPlayerModel pm = getCurrentPm(); 
        if(pm != null)
            doAsk(pm, askNextRunnable);
    }
    
    public void goOnAsk()
    {
        AbsPlayerModel pm = getNextPm(); //getNext will end it 
        if(pm != null)
            doAsk(pm, askNextRunnable);
    }
    
    public AbsPlayerModel getCurrentPm()
    {
        return (isAskEnd()==false)? askPms.get(askIndex) : null;
    }
    
    public AbsPlayerModel getNextPm()
    {
        askIndex++;
        return getCurrentPm();
    }
    
    public boolean isAskEnd()
    {
        if(askIndex + 1>askPms.size())
        {
            endAsk();
            return true;
        }
        return false;
    }
    
    public void endAsk()
    {
    	Judge.judge.postDone(doneRunnable);
        askIndex = 0;
        doneRunnable = null;
        askPms = null;
    }
}
